﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Filter
{
    /// <summary>
    /// 类型、格式转换
    /// </summary>
    public class JSConvert
    {
        /// <summary>
        /// 将像素值转换为毫米值（整数结果）
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="dpi">设备DPI</param>
        /// <returns></returns>
        public static int Pixel2Mili_I(int val, float dpi)
        {
            try
            {
                return (int)Math.Round((val * 25.4F / dpi), 0);
            }
            catch {
                return 0;
            }
        }

        /// <summary>
        /// 将像素值转换为毫米值（小数结果）
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="dpi">设备DPI</param>
        /// <returns></returns>
        public static float Pixel2Mili_F(int val, float dpi)
        {
            try
            {
                return val * 25.4F / dpi;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将毫米值转换为像素值（小数结果）
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="dpi">设备DPI</param>
        /// <returns></returns>
        public static float Mili2Pixel_F(float val, float dpi)
        {
            try
            {
                return dpi * val / 25.4F;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将毫米值转换为像素值（整数结果）
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="dpi">设备DPI</param>
        /// <returns></returns>
        public static int Mili2Pixel_I(float val, float dpi)
        {
            try
            {
                return (int)Math.Round(dpi * val / 25.4F, 0);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 像素转百分之一英寸（小数结果）
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="dpi">设备DPI</param>
        /// <returns></returns>
        public static float Pixel2Inch_F(int val, float dpi)
        {
            try
            {
                return val * 100 / dpi;
            }
            catch {
                return 0;
            }
        }

        /// <summary>
        /// 像素转百分之一英寸（小数结果）
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="dpi">设备DPI</param>
        /// <returns></returns>
        public static float Pixel2Inch_F(float val, float dpi)
        {
            try
            {
                return val * 100 / dpi;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 像素转百分之一英寸（整数结果）
        /// </summary>
        /// <param name="val">值</param>
        /// <param name="dpi">设备DPI</param>
        /// <returns></returns>
        public static int Pixel2Inch_I(int val, float dpi)
        {
            try
            {
                return (int)Math.Ceiling(val * 100 / dpi);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 毫米转英寸（小数结果）
        /// </summary>
        /// <param name="val"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        public static float Mili2Inch_F(int val, float dpi)
        {
            try
            {
                float x = Mili2Pixel_F(val, dpi);
                return Pixel2Inch_F(x, dpi);
            }
            catch {
                return 0;
            }
        }

        /// <summary>
        /// 毫米转英寸（整数结果）
        /// </summary>
        /// <param name="val"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        public static int Mili2Inch_I(int val, float dpi)
        {
            try
            {
                int x = Mili2Pixel_I(val, dpi);
                return Pixel2Inch_I(x, dpi);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将Float型的字号转为中文的字号名称或对应的String型字符串
        /// </summary>
        /// <param name="pt">字号</param>
        /// <returns></returns>
        public static string FindFontSizeName(float pt)
        {
            if (pt.Equals(42F))
                return "初号";
            else if (pt.Equals(36F))
                return "小初";
            else if (pt.Equals(26F))
                return "一号";
            else if (pt.Equals(24F))
                return "小一";
            else if (pt.Equals(22F))
                return "二号";
            else if (pt.Equals(18F))
                return "小二";
            else if (pt.Equals(16F))
                return "三号";
            else if (pt.Equals(15F))
                return "小三";
            else if (pt.Equals(14F))
                return "四号";
            else if (pt.Equals(12F))
                return "小四";
            else if (pt.Equals(10.5F))
                return "五号";
            else if (pt.Equals(9F))
                return "小五";
            else if (pt.Equals(7.5F))
                return "六号";
            else if (pt.Equals(6.5F))
                return "小六";
            else if (pt.Equals(5.5F))
                return "七号";
            else if (pt.Equals(5F))
                return "八号";
            else
                return pt.ToString();
        }

        /// <summary>
        /// 将字号名称或String类型的字号转换为Float型的字号大小
        /// </summary>
        /// <param szname="szname">字号名称或字号大小</param>
        /// <returns></returns>
        public static float FindFontSize(string szname)
        {
            if (szname.Equals("初号"))
                return 42F;
            else if (szname.Equals("小初"))
                return 36F;
            else if (szname.Equals("一号"))
                return 26F;
            else if (szname.Equals("小一"))
                return 24F;
            else if (szname.Equals("二号"))
                return 22F;
            else if (szname.Equals("小二"))
                return 18F;
            else if (szname.Equals("三号"))
                return 16F;
            else if (szname.Equals("小三"))
                return 15F;
            else if (szname.Equals("四号"))
                return 14F;
            else if (szname.Equals("小四"))
                return 12F;
            else if (szname.Equals("五号"))
                return 10.5F;
            else if (szname.Equals("小五"))
                return 9F;
            else if (szname.Equals("六号"))
                return 7.5F;
            else if (szname.Equals("小六"))
                return 6.5F;
            else if (szname.Equals("七号"))
                return 5.5F;
            else if (szname.Equals("八号"))
                return 5F;
            else
            {
                if (JSFormat.IsPositiveNumber(szname, ""))
                    return float.Parse(szname);
                else
                    return -1.0F;
            }
        }

        /// <summary>
        /// 获取文本字体样式（根据条件拼接）
        /// </summary>
        /// <param name="isBold">是否粗体</param>
        /// <param name="isItalic">是否斜体</param>
        /// <param name="isStrikout">是否删除线</param>
        /// <param name="isUnderline">是否下划线</param>
        /// <returns></returns>
        public static FontStyle GetFontStyle(bool isBold, bool isItalic, bool isStrikout, bool isUnderline)
        {
            FontStyle fs = new FontStyle();

            List<FontStyle> fsArr = new List<FontStyle>();

            if (isBold)
                fsArr.Add(FontStyle.Bold);
            if (isItalic)
                fsArr.Add(FontStyle.Italic);
            if (isStrikout)
                fsArr.Add(FontStyle.Strikeout);
            if (isUnderline)
                fsArr.Add(FontStyle.Underline);

            for (int i = 0; i < fsArr.Count; i++)
            {
                if (i == 0)
                    fs = fsArr[i];
                else
                    fs = fs | fsArr[i];
            }

            return (FontStyle)(fs);
        }
    }
}
